from rest_framework.views import exception_handler as drf_exception_handler
from rest_framework.response import Response

from rest_framework import status

def custom_exception_handler(exc, context):
    """
    自定义异常处理函数
    :param exc: 异常对象，本次发生的异常对象
    :param context: 字典，异常出现时的执行上下文环境
    :return:
    """
    # 先让drf进行异常判断
    response = drf_exception_handler(exc, context)

    # 判断response对象是否为None
    if response is None:
        """出现drf不能处理的异常"""

        if isinstance(exc, ZeroDivisionError):

            return Response({"error": "0不能作为除数!"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

    return response